package lj.courseinterect.rest.resources;

import lj.courseinterect.dao.i.CourseInfoDao;
import lj.courseinterect.dao.i.LocateInfoDao;
import lj.courseinterect.dao.i.StudentCourseDao;
import lj.courseinterect.dao.i.StudentDao;
import lj.courseinterect.entity.CourseInfo;
import lj.courseinterect.entity.LocateInfo;
import lj.courseinterect.entity.Student;
import lj.courseinterect.entity.StudentCourse;
import lj.courseinterect.transfer.LocateTransfer;
import lj.courseinterect.transfer.OperationTransfer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Component
@Path("/{a:tea|stu}/locates")
public class LocateInfoResource
{
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private LocateInfoDao locateInfoDao;

    @Autowired
    private CourseInfoDao courseInfoDao;

    @Autowired
    private StudentCourseDao studentCourseDao;

    @Autowired
    private StudentResource studentResource;

    @Autowired
    private StudentDao studentDao;

    public Logger getLogger() {
        return logger;
    }

    public LocateInfoDao getLocateInfoDao() {
        return locateInfoDao;
    }

    public void setLocateInfoDao(LocateInfoDao locateInfoDao) {
        this.locateInfoDao = locateInfoDao;
    }

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public OperationTransfer create(LocateInfo locateInfo)
    {
        Long stuId = studentResource.getStudent().getStuId();
        locateInfo.setStuId(stuId);
        //选课学生合法
        StudentCourse studentCourse = studentCourseDao.findByStuIdAndCouId(locateInfo.getStuId(),locateInfo.getCouId());
        OperationTransfer operationTransfer;

        if (studentCourse != null){
            //学生已经选课
            CourseInfo courseInfo = courseInfoDao.find(locateInfo.getCouId());
            if(courseInfo != null && courseInfo.getLocState() == CourseInfo.LocState.OPEN){
                if(locateInfoDao.findByStuIdAndCouId(stuId, locateInfo.getCouId()) == null){
                    try {
                        this.locateInfoDao.save(locateInfo);
                        studentCourse.setLocCount(studentCourse.getLocCount() + 1);
                        studentCourseDao.save(studentCourse);

                        operationTransfer = new OperationTransfer(true, "签到成功");
                    } catch (Exception ex) {
                        operationTransfer = new OperationTransfer(false, "签到失败");
                    }
                } else {
                    operationTransfer = new OperationTransfer(false, "已经签到过");
                }
            } else {
                operationTransfer = new OperationTransfer(false, "签到尚未开始");
            }

            this.logger.info("create(): " + operationTransfer.toString());
            return operationTransfer;
        }else{
            return new OperationTransfer(false, "学生未选课");
        }
    }

    /**
     * 通过课程获得签到信息
     * @param couId
     * @return
     */
    @GET
    @Path("getLocateByCouId/{couId}")
    @Produces(MediaType.APPLICATION_JSON)
    public List<LocateTransfer> getLocateByCouId(@PathParam("couId") Long couId){
        List<LocateTransfer> locateTransfers = new ArrayList<LocateTransfer>();

        List<LocateInfo> locateInfos = locateInfoDao.queryByCouId(couId);
        if(locateInfos == null){
            return null;
        }
        for(LocateInfo locateInfo : locateInfos){
            Student student = studentDao.find(locateInfo.getStuId());

            LocateTransfer locateTransfer = new LocateTransfer();
            locateTransfer.setStudent(student);
            locateTransfer.setLocateInfo(locateInfo);

            locateTransfers.add(locateTransfer);
        }

        return locateTransfers;
    }


    @POST
    @Path("setLocateState/{couId}")
    @Produces(MediaType.APPLICATION_JSON)
    public OperationTransfer setLocateState(@PathParam("couId") Long couId,
                                            @FormParam("locState") CourseInfo.LocState locState){
        OperationTransfer operationTransfer;
        CourseInfo courseInfo = courseInfoDao.find(couId);
        if(courseInfo != null && !courseInfo.getLocState().equals(locState)){
            courseInfo.setLocState(locState);
            try{
                if(locState.equals(CourseInfo.LocState.OPEN)){
                    locateInfoDao.deleteByCouId(couId);
                    courseInfo.setSignCount(courseInfo.getSignCount() + 1);
                }
                courseInfoDao.save(courseInfo);
                operationTransfer = new OperationTransfer(true, "操作成功");
            } catch (Exception ex){
                ex.printStackTrace();
                operationTransfer = new OperationTransfer(false, "操作失败");
            }

        } else {
            operationTransfer = new OperationTransfer(false, "不能执行该操作");
        }

        return operationTransfer;
    }
}